package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.constant.MessageConstant;
import org.example.constant.RoleIdConstant;
import org.example.constant.UserMessageConstant;
import org.example.dao.entity.UserRoles;
import org.example.dao.mapper.RolesMapper;
import org.example.dao.mapper.UserRolesMapper;
import org.example.exception.BindFailedException;
import org.example.exception.NoPermissionException;
import org.example.exception.PermissionException;
import org.example.result.Result;
import org.example.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;

@Service
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<UserRolesMapper, UserRoles> implements PermissionService {

    private final UserRolesMapper userRolesMapper;


    @Resource
    private RolesMapper rolesMapper;

    public PermissionServiceImpl(UserRolesMapper userRolesMapper) {
        this.userRolesMapper = userRolesMapper;
    }


    @Override
    public Result bindDefaultRole(BigInteger userId) {
        UserRoles userRoles = UserRoles.builder()
                .userId(userId)
                .roleId(BigInteger.valueOf(RoleIdConstant.USER))
                .build();
        int insert = userRolesMapper.insert(userRoles);
        //判断是否绑定成功
        if (insert == 0) {
            throw new BindFailedException(MessageConstant.BIND_DEFAULT_ROLE_FAILED);
        }
        log.info(MessageConstant.BIND_DEFAULT_ROLE_SUCCESS);
        return Result.success();
    }

    @Override
    public String getUserRoleCode(BigInteger userId) {
//        QueryWrapper<UserRoles> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(UserMessageConstant.USER_ID, userId);
//        UserRoles userRoles = userRolesMapper.selectOne(queryWrapper);
//        BigInteger roleId = userRoles.getRoleId();
//        QueryWrapper<Roles> qw = new QueryWrapper<>();
//        qw.eq(UserMessageConstant.ROLE_ID, roleId);
//        Roles roles = rolesMapper.selectOne(qw);

        //用mapper不知道为什么一直加载不成功 找不到方法 各种问题都看了 还是没有解决 路径问题也排查了 仍然是识别不到 不知道怎么回事...
        String code = userRolesMapper.selectRoleCodeByUserId(userId);

        return code;
    }

    @Override
    public Result upgradeToAdmin(BigInteger targetUserId, BigInteger currentUserId) {
        //获取目标用户角色
        String targetRoleCode = userRolesMapper.selectRoleCodeByUserId(targetUserId);
        String currentroleCode = userRolesMapper.selectRoleCodeByUserId(currentUserId);
        if (!UserMessageConstant.SUPER_ADMIN.equals(currentroleCode)) {
            return Result.error(MessageConstant.NO_PERMISSION);
        }

        if (UserMessageConstant.ADMIN.equals(targetRoleCode)) {
            throw new PermissionException(MessageConstant.USER_IS_ADMIN);
        }
        if (UserMessageConstant.SUPER_ADMIN.equals(targetRoleCode)) {
            throw new PermissionException(MessageConstant.USER_IS_SUPER_ADMIN);
        }
        UserRoles userRoles = UserRoles.builder()
                .userId(targetUserId)
                .roleId(BigInteger.valueOf(RoleIdConstant.ADMIN))
                .build();
        QueryWrapper<UserRoles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(UserMessageConstant.USER_ID, targetUserId);
        userRolesMapper.update(userRoles, queryWrapper);
        return Result.success();
    }

    @Override
    public Result downgradeToUser(BigInteger targetUserId, BigInteger currentUserId) {
        //获取目标用户角色
        String targetRoleCode = userRolesMapper.selectRoleCodeByUserId(targetUserId);
        String currentroleCode = userRolesMapper.selectRoleCodeByUserId(currentUserId);
        if (!UserMessageConstant.SUPER_ADMIN.equals(currentroleCode)) {
            throw new NoPermissionException(MessageConstant.NO_PERMISSION);
        }
        if (UserMessageConstant.USER.equals(targetRoleCode)) {
            throw new PermissionException(MessageConstant.USER_IS_USER);
        }
        if (UserMessageConstant.SUPER_ADMIN.equals(targetRoleCode)) {
            throw new PermissionException(MessageConstant.NO_PERMISSION);
        }
        UserRoles userRoles = UserRoles.builder()
                .userId(targetUserId)
                .roleId(BigInteger.valueOf(RoleIdConstant.USER))
                .build();
        QueryWrapper<UserRoles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(UserMessageConstant.USER_ID, targetUserId);
        userRolesMapper.update(userRoles,queryWrapper);
        return Result.success();
    }

    @Override
    public void bindSuperAdminRole(BigInteger userId) {
        UserRoles userRoles = UserRoles.builder()
                .userId(userId)
                .roleId(BigInteger.valueOf(RoleIdConstant.SUPER_ADMIN))
                .build();
        userRolesMapper.insert(userRoles);
    }

}
